package engine.controllers;

import misc.Debug;
import ai.AIUtil;
import ai.engine.AIPlayer;
import engine.GameEngine;
import engine.data.Building;
import engine.data.City;
import engine.data.Player;
import engine.data.Technology;
import engine.data.Unit;
import engine.data.UnitType;
import engine.graphics.popup.PopupCity;
import engine.graphics.popup.PopupManager;
import engine.graphics.popup.PopupOK;

/**
 * This class is the access point of game engine for a human player.
 * 
 */
public final class HumanController extends VisiblePlayerController {

	Player player;

	/**
	 * Constructs a Human controller
	 * 
	 * @param gamePlayer
	 */
	public HumanController(final Player gamePlayer) {
		super();
		this.player = gamePlayer;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see PlayerController#onNewBuilding()
	 */
	public void onNewBuilding(final City city, final Class buildingType, final byte buildingId) {
		Debug.logConsole("onNewBuilding (Human)");
		StringBuffer sb = new StringBuffer();
		String name = "";
		String info = "";
		if (city.buildingType.equals(Building.class)) {
			name = Building.getName(city.buildingId);
			info = Building.getInfo(city.buildingId);
			city.buildingRoster.add(new Byte(city.buildingId));
		}
		if (city.buildingType.equals(Unit.class)) {
			name = UnitType.getName(city.buildingId);
			// info = Building.getInfo(city.buildingId);
		}
		sb.append(city.name).append(" has built :\n").append(name).append("\n \n").append(info);
		PopupManager.showPopupOK(PopupOK.POPUP_CITY, sb.toString(), city);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see PlayerController#onNewCity(City city)
	 */
	public void onNewCity(final City city) {
		Debug.logConsole("onNewCity (Human)");
		PopupManager.showPopupCity(city);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see PlayerController#onChooseBuilding(City city)
	 */
	public void onChooseBuilding(final City city) {
		Debug.logConsole("onChooseBuilding (Human)");
		PopupManager.showPopupCity(city);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see engine.controllers.PlayerController#onNewPeople(engine.data.City)
	 */
	public void onNewPeople(final City city) {
		Debug.logConsole("onNewPeople  (Human)");
		PopupManager.showPopupCity(city, PopupCity.MAP_PANEL);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see engine.controllers.PlayerController#onNextTechnology()
	 */
	public void onNextTechnology() {
		Debug.logConsole("onNextTechnology (Human)");
		if (player.technology != Technology.NONE) {
			StringBuffer sb = new StringBuffer();
			sb.append("You have discovered:\n").append(Technology.getName(GameEngine.currPlayer.technology));
			PopupManager.showPopupOK(PopupOK.POPUP_TECHNOLOGY, sb.toString(), null);
		} else {
			PopupManager.showPopupTechnology();
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see engine.controllers.PlayerController#onDiplomacyMeeting()
	 */
	public void onDiplomacyMeeting() {
		PopupManager.showPopupUnitOrders();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see engine.controllers.PlayerController#onTradePoint()
	 */
	public void onTradePoint() {
		PopupManager.showPopupUnitOrders();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see PlayerController#onEndOfTurn()
	 */
	public void onEndOfTurn() {
		//
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see engine.controllers.PlayerController#onEndUnitTurn()
	 */
	public void onEndUnitTurn() {
		Unit unit = GameEngine.currPlayer.currUnit;
		
		Debug.logConsole("HUMAN CONTROLLER EndUnitTurn");
		for(int i=0;i<GameEngine.players.size();i++){
			if (GameEngine.players.get(i)!=GameEngine.currPlayer && GameEngine.players.get(i) instanceof AIPlayer){
				AIPlayer aiPlayer = (AIPlayer)GameEngine.players.get(i);
				if (GameEngine.map.isSeen(aiPlayer,unit.x, unit.y) && !aiPlayer.enemyUnits.contains(unit)){
					Debug.logConsole("AI EVENT - New Enemy Unit Added by Human Controller");
					aiPlayer.enemyUnits.add(unit);
				}
			}
		}

	}

}
